/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Structures;

import java.io.File;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Mariusz
 */
public class AuchanProfile extends ReceiptProfile implements Serializable {
    public AuchanProfile() {
        super("Auchan");
        this.addTrainingImage(new File("./pliki/auchan/przecdodwukropek.png"), ',', ':');
        this.addTrainingImage(new File("./pliki/auchan/duzee.png"), 'A', 'Z');
        this.addTrainingImage(new File("./pliki/auchan/malee.png"), 'a', 'z');
        this.addTrainingImage(new File("./pliki/auchan/spachasz.png"), '!', '#');
    }

    @Override
    public boolean isDefault() {
        return true;
    }
    
    @Override
    public boolean checkIfProduct(String p) {
        return p.matches(".+[ABCc]{1}.+[xX]{1}.+\\s{1}[ABCc]{1}\\s{1}") ? true : false;
    }

    @Override
    public Entry getProduct(String s) {
        Entry product = new Entry();
        if(s.matches(".+\\s+[0-9iloszILOSZ,.]{3,}kg\\s{1}.+\\s{1}")){
            String weight = "";
            String name = "";
            String price = "";

            Matcher matcherWight = Pattern.compile("\\s+[0-9iloszILOSZ,.]{3,}kg\\s{1}").matcher(s);
            while (matcherWight.find()) {
                weight = matcherWight.group();
            }
            weight = weight.replace('o', '0');
            weight = weight.replace('O', '0');
            weight = weight.replace('z', '2');
            weight = weight.replace('Z', '2');
            weight = weight.replace('S', '8');
            weight = weight.replace('s', '8');
            weight = weight.replace('l', '1');
            weight = weight.replace('i', '1');
            weight = weight.replace('I', '1');
            weight = weight.replaceAll("kg", "");
            weight = weight.replaceAll("\\s", "");
            weight = (weight.length() >= 4) ?
                new StringBuffer(weight).insert(weight.length() - 3, '.').toString() :
                new StringBuffer(weight).insert(weight.length() - 2, '.').toString();

            String[] nameAndPrice = s.split("\\s+[0-9iloszILOSZ,.]{3,}kg\\s{1}", 2);
            name = nameAndPrice[0];
            price = nameAndPrice[1];
            price = price.replace('o', '0');
            price = price.replace('O', '0');
            price = price.replace('z', '2');
            price = price.replace('Z', '2');
            price = price.replace('S', '8');
            price = price.replace('s', '8');
            price = price.replace('l', '1');
            price = price.replace('i', '1');
            price = price.replace('I', '1');

            Matcher matcherPrice = Pattern.compile("[xX][0-9,.]+\\s{1,}").matcher(price);
            while (matcherPrice.find()) {
                price = matcherPrice.group();
            }
            price = price.replace('x', '0');
            price = price.replace('X', '0');

            price = price.replaceAll("[^0-9]", "");
            price = new StringBuffer(price).insert(price.length() - 2, '.').toString();

            product.name = name;
            product.price = Float.parseFloat(price);
            product.quantity = Float.parseFloat(weight);
        }else{
            String name = "";
            String price = "";
            Matcher priceAmount = Pattern.compile("\\s{1,}[0-9iloszILOSZ,.]+[xX][0-9iloszILOSZ,.]+\\s{1,}").matcher(s);
            while (priceAmount.find()) {
                price = priceAmount.group();
            }
            price = price.replace('o', '0');
            price = price.replace('O', '0');
            price = price.replace('z', '2');
            price = price.replace('Z', '2');
            price = price.replace('S', '8');
            price = price.replace('s', '8');
            price = price.replace('l', '1');
            price = price.replace('i', '1');
            price = price.replace('I', '1');
            price = price.replaceAll("[^0-9xX]", "");
            String[] priceAmountTemp = price.split("[xX]", 2);

            String[] nameAndPrice = s.split("\\s{1,}[0-9iloszILOSZ]+[xX][0-9iloszILOSZ]+\\s{1,}", 2);
            name = nameAndPrice[0];

            priceAmountTemp[1] = new StringBuffer(
                    priceAmountTemp[1]).insert(priceAmountTemp[1].length() - 2, '.').toString();

            
            product.name = name;
            product.price = Float.parseFloat(priceAmountTemp[1]);
            product.quantity = Float.parseFloat(priceAmountTemp[0]);
        }
        
        return product;
    }
}
